"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 数据库兼容--兼容b库语法
Case Name   : 兼容a库无dolphin插件，插入数据，省略into，合理报错
Create At   : 2022.10.14
Owner       : z@wan005
Description :
    1.创建a库
    2.创建表
    3.插入数据一条数据，省略into
    4.清理环境
Expect      :
    1.创建成功
    2.成功
    3.合理报错
    4.清理环境完成
History     :
"""

import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Compatibility(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.tb_name = 'tb_grammar_case0049'
        self.db_name = 'db_grammar_case0049'

    def test_grammar(self):
        text = '---step1:创建a库;expect:创建成功---'
        self.log.info(text)
        sql_cmd1 = f"drop database if exists {self.db_name};" \
                   f"create database {self.db_name} dbcompatibility ='a';"
        sql_res = self.pri_sh.execut_db_sql(sql_cmd1)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res,
                      '执行失败:' + text)

        text = '---step2:创建表;expect:成功---'
        self.log.info(text)
        sql_cmd2 = f"drop table if exists {self.tb_name};" \
                   f"create table {self.tb_name}(a text,b text, c serial);"
        sql_res = self.pri_sh.execut_db_sql(sql_cmd2, dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertIn(self.constant.TABLE_CREATE_SUCCESS, sql_res,
                      '执行失败:' + text)

        text = '---step3:插入数据一条数据，省略into;expect:报错---'
        self.log.info(text)
        sql_cmd2 = f"insert {self.tb_name} values('测试1','测试2*&^',default);"
        sql_res = self.pri_sh.execut_db_sql(sql_cmd2, dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertIn('ERROR:  syntax error', sql_res, '执行失败:' + text)

    def tearDown(self):
        text = '---step4:清理环境;expect:清理环境完成---'
        self.log.info(text)
        drop_cmd1 = f"drop table {self.tb_name};"
        drop_res = self.pri_sh.execut_db_sql(drop_cmd1,
                                             dbname=f'{self.db_name}')
        self.log.info(drop_res)
        drop_msg = self.pri_sh.execut_db_sql(f"drop database if exists "
                                             f"{self.db_name};")
        self.log.info(drop_msg)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, drop_res)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, drop_msg,
                      '执行失败:' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
